<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" :modal-append-to-body="true" :append-to-body="true"  class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="22">
          <el-form-item label="标题" prop="title" :rules="rules.validateRequire">
            <el-input v-model="model.title" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="" prop="name" label-width="5px">
            <el-button type="text" icon="el-icon-connection" @click="openRefFile" v-if="model.relationsystem === '0'">关联文件</el-button>
            <el-button type="text" icon="el-icon-connection" @click="openTest1" v-else >关联文件</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
				<el-col :span="12">
					<el-form-item label="关联系统" prop="relationsystem" >
<!--						<el-input v-model="model.relationsystem"></el-input>-->
            <el-select v-model="model.relationsystem" filterable clearable>
              <el-option v-for="item in codeModel.relationSystemList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
					</el-form-item>
				</el-col>
        <el-col :span="12">
          <el-form-item label="关联系统来源类型" prop="relationsourcetype" >
            <el-input v-model="model.relationsourcetype" disabled></el-input>
          </el-form-item>
        </el-col>
			</el-row>
			<el-row>
        <el-col :span="12">
          <el-form-item label="关联系统来源id" prop="relationsourceid" >
            <el-input v-model="model.relationsourceid" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="文件最后更新时间" prop="lastupdatetime" >
            <el-date-picker v-model="model.lastupdatetime" type="datetime" placeholder="文件最后更新时间" value-format="yyyy-MM-dd HH:mm:ss" disabled></el-date-picker>
          </el-form-item>
        </el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="备注" prop="description" >
						<el-input v-model="model.description" type="textarea" :rows="4"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="排序码" prop="sortcode">
            <el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效" prop="enabled">
            <el-radio-group v-model="model.enabled">
              <el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
    <div  v-show="showFileFlag" style="height:100vh;width:100vw;position:fixed;z-index:1020;top:0;left:0">
      <tl-iframe v-if="display" ref="frame1" :url="localUrl" path="cdmc/fileInfoManage/fileSelectComponent" :selectMode="'single'" :systemParamCode="'CDMC_LTC_FLOW_FILE'" :startMethod="'open'" @result="doResult" :data="{ visible: true }" @close="cancel" height="100%"></tl-iframe>
    </div>
    <ref-file-list ref="refFileList" @confirm="selectFile"></ref-file-list>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import refFileList from "./fileRefList"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'

/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { refFileList },
  data() {
    return {
      moduleTitle: '文档配置',
      urlPrefix: '/base/processDocumentInfo/',
      url: {
        // 额外的url
      },
      display: true,
      showFileFlag: false,
      localUrl: process.env.CDMC_API + '/dynamic'
    }
  },
  props: {
    // 组类别作为关联编码
    category: {
      default: '',
      type: String
    }
  },
  methods: {
    close() {
    },
    openTest1() {
      this.showFileFlag = true
    },
    cancel() {
      this.display = false
      this.display = true
      this.showFileFlag = false
      this.$nextTick(() => {
        this.$refs.frame1.open()
      })
    },
    loaded() {
      // 页面加载完毕执行
    },
    openRefFile() {
      this.$refs.refFileList.visible = true
    },
    selectFile(selectedRows, selectedKeys, selectedValues) {
      this.$set(this.model, "title", selectedRows.name)
      this.$set(this.model, "lastupdatetime", selectedRows.modifiedon)
      // sourcetype暂时定为OA文档管理表，若之后接入其它系统则再进行适配
      this.$set(this.model, "relationsourcetype", 'filedirectorymanage')
      this.$set(this.model, "relationsourceid", selectedRows.id)
      this.$set(this.model, "usercode", this.$store.getters.user.code)
      this.$set(this.model, "username", this.$store.getters.user.name)
    },
    doResult(fileList) {
      if (fileList && fileList.length > 0) {
        let file = fileList[0]
        this.$set(this.model, "title", file.name)
        this.$set(this.model, "lastupdatetime", file.submitdate)
        // sourcetype暂时定为OA文档管理表，若之后接入其它系统则再进行适配
        this.$set(this.model, "relationsourcetype", 'FileInfoManage')
        this.$set(this.model, "relationsourceid", file.fileinfoid)
        this.$set(this.model, "usercode", this.$store.getters.user.code)
        this.$set(this.model, "username", this.$store.getters.user.name)
      }
    },
    /** 默认值 */
    assignDefaultValue() {
      this.$set(this.model, 'relationsystem', '1')
    },
    open() {
      this.$nextTick((_) => {
        this.$refs.frame1.open()
      })
      // 打开后将父组件传过来的category赋给model
      this.model = {}
      this.$set(this.model, 'category', this.category)
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '添加' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then((_) => {
              this.assignDefaultValue()
              this.$refs.form.clearValidate()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.$refs.form.clearValidate()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.$refs.form.clearValidate()
            this.loaded()
          })
        }
      })
    }
  }
}
</script>

